package pool;

import java.util.concurrent.*;

public class ThreaPoolDemo {

    public static void main(String[] args) {
        //自定义线程池
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
                2,     // 核心线程数
                2,  // 最大线程数
                0L,    // 空闲线程存活时间
                TimeUnit.SECONDS,   // 时间单位
                new LinkedBlockingQueue<>(5), // 有界队列！
                Executors.defaultThreadFactory(),// 线程工厂
                new ThreadPoolExecutor.CallerRunsPolicy() // 拒绝策略，由调用线程处理
        );

        for (int i = 0; i < 10; i++) {
          poolExecutor.submit(new Runnable() {
              @Override
              public void run() {
                  System.out.println(Thread.currentThread().getName());
              }
          });

          Future<Integer> submit = poolExecutor.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                return 1;
            }
          });
        }
        poolExecutor.shutdown();
        /*
            拒绝策略：
                    1. AbortPolicy() 默认拒绝策略，抛出异常
                    2. DiscardPolicy() 丢弃任务
                    3. DiscardOldestPolicy() 丢弃队列中最老的任务
                    4. CallerRunsPolicy() 由调用线程处理
        */
    }
}
